#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

struct row{
    char d1;
    char d2;
    char d3;
};

// char nums[] = {-1,0,1,2,-1,-4,5,-2,-1};
// char nums[] = {-1,0,1,2,-1,-4};
struct row result[100];
unsigned int index_result = 0;

int cmpfunc (const void * a, const void * b)
{
   return ( *(char*)a - *(char*)b );
}

int is_char_in_str(char c,char *str,int len) {
    for(int i = 0; i < len; i++){
        if(c == str[i]) return 1;
    }

    return 0;
}

int child_same_judge(char *str1,char *str2,int len){
    char *str1_backup,*str2_backup;

    str1_backup = str1;
    str2_backup = str2;

    for(int i=0;i<len;i++){
        if(!is_char_in_str(str1[i],str2_backup,3)) return 0;
    }

    for(int i=0;i<len;i++){
        if(!is_char_in_str(str2[i],str1_backup,3)) return 0;
    }

    return 1;
}

int is_str_in_queue(char *str,struct row *queue,int len){
    for(int i=0;i<len;i++){
        if(child_same_judge(str,(char *)queue,3)) 
            return 1;
        queue++;
    }
    return 0;
}

int str_sum(char *str,int len){
    int sum = 0;
    for(int i=0;i<len;i++){
        sum+=*str;
        str++;
    }
    return sum;
}

int test(void)
{
    char c = 9;
    char t1[] = {1,2,3};
    printf("the len of t1 is %d\n",sizeof(t1));
    if(is_char_in_str(c,t1,sizeof(t1))) {
        printf("char c in t1\n");
    }
    else{
        printf("char c not in t1\n");
    }
}

int main(int argc, char **argv)
{
    int ct=0;
    clock_t start_dida,finish_dida;
    long long int runtimer;

    char sample_dat[3]= {0};
    char *nums;

    nums = malloc(argc-1);
    for(int i=0; i<argc-1; i++){
        nums[i] = (char)atoi(argv[i+1]);
    }

    start_dida = clock();
    qsort(nums,argc-1,sizeof(char),cmpfunc);

    // for(int i=0; i<argc-1;i++){
    //     printf("the %d's num is %d\n",i,nums[i]);
    // }


    for(int i=0; i<argc-1; i++){
        int lp = i+1;
        int rp = argc-1-1;

        if(0 <= nums[i]) break;
        while(lp<rp){
            if(0 == nums[i]+nums[lp]+nums[rp]){
                result[index_result].d1 = nums[i]; 
                result[index_result].d2 = nums[lp]; 
                result[index_result].d3 = nums[rp]; 
                index_result++;
                while(nums[lp]==nums[lp+1]){
                    lp+=1;
                }
                while(nums[rp]==nums[rp-1]){
                    lp-=1;
                }
                lp++;
                rp--;
            }
            else if(0 < nums[i]+nums[lp]+nums[rp]){
                rp--;
            }
            else{
                lp++;
            }
        }
        if(nums[i]==nums[i+1]) i++;
    }


    finish_dida = clock();
    runtimer = finish_dida-start_dida;
    // printf("run timer %lli dida\n",runtimer);
    // printf("dida per sec is %li \n",CLOCKS_PER_SEC);
    printf("run time %lli us\n",runtimer*1000000/CLOCKS_PER_SEC);

    for(int i=0; i<index_result;i++)
        printf("the %d's result is : %d,%d,%d \n",i,result[i].d1,result[i].d2,result[i].d3);
}